package com.scgaopan.disruptor;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * Author:scgaopan
 * Date:18/10/25
 * Description:
 */
public class LongEventProducerWithTranslator {

    private final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }


    private static final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR =
            new EventTranslatorOneArg<LongEvent, ByteBuffer>() {
                /**
                 * translateTo方法将ringbuffer中消息，转换成java对象格式
                 * @param longEvent
                 * @param sequece
                 * @param byteBuffer
                 */
                public void translateTo(LongEvent longEvent, long sequece, ByteBuffer byteBuffer) {
                    longEvent.setValue(byteBuffer.getLong(0));
                }
            };

    /**
     * 将生产者生产的消息放入ringbuffer中
     * @param bb
     */
    public void product(ByteBuffer bb) {
        ringBuffer.publishEvent(TRANSLATOR, bb);
    }

}
